﻿@page "/Scheduled_Tasks"
@using WorkFlow.Data
@using WorkFlow.Data.Model
@inject CommentTool commentTool
@inject List<ScheduledTaskEntity> scheduledTask
@inject IJSRuntime JSRunTime
@inject DataManager dataManager
<h3>Scheduled_Tasks</h3>

<div>
    PlatformName&nbsp;<input type="text" id="PlatformName">
    CompanyName&nbsp;<input type="text" id="CompanyName">
    TaskName&nbsp;<input type="text" id="TaskName" @bind="@_Name">
    &nbsp;&nbsp;<button @onclick="@(()=>Search())" class="btn btn-primary btn-xs">Search</button>
    &nbsp;&nbsp;<button @onclick="@(()=>Add())" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#EditTask">Add Task</button>
</div>
<br />
<div>
    <table class="table">
        <thead>
            <tr>
                <th>Sno</th>
                <th>PlatformName</th>
                <th style="width: 15%;">CompanyName</th>
                <th style="width: 20%;">Name</th>
                <th style="width: 20%;">WorkFlowName</th>
                <th style="width: 40%;">Description</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            @if (scheduledTask != null)
            {
                @for (var i = 0; i < scheduledTask.Count; i++)
                {
                    ScheduledTaskEntity taskEntity = scheduledTask[i];
                    <tr>
                        <td>@(i+1)</td>
                        <td></td>
                        <td></td>
                        <td><a @onclick="()=>HiddenNav()" href="TaskDetail/@taskEntity.WorkFlowName/@taskEntity.ID" target="_RuleDetail">@taskEntity.Name</a></td>
                        <td>@taskEntity.WorkFlowName</td>
                        @{
                            string description = "";
                            switch (@taskEntity.Type)
                            {
                                case 1:
                                    description = $"Start 【{@taskEntity.WorkFlowName}】 workflow at {GetTime(@taskEntity)} every day";
                                    break;
                                case 2:
                                    description = $"Start 【{@taskEntity.WorkFlowName}】 workflow at {GetTime(@taskEntity)} every {GetWeek(@taskEntity.Day)}";
                                    break;
                                case 3:
                                    description = $"Start 【{@taskEntity.WorkFlowName}】 workflow at {GetTime(@taskEntity)} on the {@taskEntity.Day}rd of each month";
                                    break;
                                case 4:
                                    description = $"Start 【{@taskEntity.WorkFlowName}】 workflow at {GetTime(@taskEntity)} on the {@taskEntity.Day}rd of each year";
                                    break;
                                case 5:
                                    description = $"Start 【{@taskEntity.WorkFlowName}】 workflow every {GetIntervalTime(@taskEntity)} ";
                                    break;
                                default:
                                    break;
                            }
                            <td>@description</td>
                        }
                        <td><button class="btn btn-outline-primary" @onclick="@(()=>EditTask(@taskEntity))" data-toggle="modal" data-target="#EditTask">Edit</button></td>
                        <td><button class="btn btn-primary" @onclick="@(()=>DeleteTask(@taskEntity))">Delete</button></td>
                    </tr>
                }
            }
        </tbody>
    </table>
</div>

<div class="modal fade" id="EditTask" tabindex="-1" role="dialog" aria-labelledby="ParamertersLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="title">Task</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="model-body">
                <div style="height:15px;"></div>
                <div>
                    &ensp;PlatformName&nbsp;
                    <select id="Search_Status" @bind="@_EditTaskEntity.PlatformID">
                    </select>
                    &ensp;CompanyName&nbsp;
                    <select id="Search_Status" @bind="@_EditTaskEntity.CompanyID">
                    </select>
                </div>
                <br>
                @if (@_EditTaskEntity != null)
                {
                    <div>
                        &ensp;TaskName &nbsp;
                        <input type="text" id="Name" @bind="@_EditTaskEntity.Name" style="width: 77%;">
                    </div>
                    <br>
                    <div>
                        &ensp;WorkFlowName &nbsp;
                        <input type="text" id="Name" @bind="@_EditTaskEntity.WorkFlowName" style="width: 69%;">
                    </div>
                    <br>
                    <div>
                        &ensp;ScheduledType &nbsp;
                        <select id="Search_Status" @bind="@_EditTaskEntity.Type">
                            <option selected="selected" value="1">Every day</option>
                            <option value="2">Every Week</option>
                            <option value="3">Every Month</option>
                            <option value="4">Every Year</option>
                            <option value="5">Every Interval Time</option>
                        </select>
                    </div>
                    <br>
                }
                @if (@_EditTaskEntity.Type == 5 || @_EditTaskEntity.Type == 4)
                {
                    <div style="float:left;">
                        &ensp;Month&nbsp;<select id="Dialog_Month" @bind="@_EditTaskEntity.Month">
                            @for (var i = 1; i < 13; i++)
                            {
                                string valueStr = (i <= 9) ? ("0" + i) : i.ToString();
                                <option value="@i">@valueStr</option>
                            }
                        </select>
                    </div>
                }
                @if (@_EditTaskEntity.Type == 5 || @_EditTaskEntity.Type == 4 || @_EditTaskEntity.Type == 3)
                {
                    <div style="float:left;">
                        &ensp;Day&nbsp;<select id="Dialog_Day" @bind="@_EditTaskEntity.Day">
                            @for (var i = 1; i < 32; i++)
                            {
                                string valueStr = (i <= 9) ? ("0" + i) : i.ToString();
                                <option value="@i">@valueStr</option>
                            }
                        </select>
                    </div>
                }
                @if (@_EditTaskEntity.Type == 2)
                {
                    <div style="float:left;">
                        &ensp;Day&nbsp;<select id="Dialog_Day" @bind="@_EditTaskEntity.Day">
                            @for (var i = 1; i < 8; i++)
                            {
                                string valueStr = GetWeek(i);
                                <option value="@i">@valueStr</option>
                            }
                        </select>
                    </div>
                }
                <div>
                    &ensp;Hour&nbsp;<select id="Dialog_Hour" @bind="@_EditTaskEntity.Hour">
                        @for (var i = 0; i < 24; i++)
                        {
                            string valueStr = (i <= 9) ? ("0" + i) : i.ToString();
                            <option value="@i">@valueStr</option>
                        }
                    </select>
                    &ensp;Minute&nbsp;<select id="Dialog_Minute" @bind="@_EditTaskEntity.Minute">
                        @for (var i = 0; i < 60; i++)
                        {
                            string valueStr = (i <= 9) ? ("0" + i) : i.ToString();
                            <option value="@i">@valueStr</option>
                        }
                    </select>
                    &ensp;Second&nbsp;<select id="Dialog_Second" @bind="@_EditTaskEntity.Second">
                        @for (var i = 0; i < 60; i++)
                        {
                            string valueStr = (i <= 9) ? ("0" + i) : i.ToString();
                            <option value="@i">@valueStr</option>
                        }
                    </select>
                </div>
                <div style="height:15px;"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">close</button>
                <button type="button" class="btn btn-primary" @onclick="Save">Save</button>
            </div>
        </div>

    </div>
</div>
@code {
    private IJSObjectReference module;
    private int oprationType = 0; //0:Add 1:Edit
    string _Name;
    ScheduledTaskEntity _EditTaskEntity = new ScheduledTaskEntity();
    protected override Task OnInitializedAsync()
    {
        commentTool.ShowNav = true;
        return base.OnInitializedAsync();
    }
    protected override async void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            module = await JSRunTime.InvokeAsync<IJSObjectReference>("import", "/vendor/MyJS.js");
            Search();
        }
    }
    private void Add()
    {
        oprationType = 0;
        _EditTaskEntity = new ScheduledTaskEntity();
    }
    private void EditTask(ScheduledTaskEntity taskEntity)
    {
        _EditTaskEntity = taskEntity;
        oprationType = 1;
    }
    private void DeleteTask(ScheduledTaskEntity taskEntity)
    {
        dataManager.DeleteScheduledTasks(taskEntity.ID);
        scheduledTask.Remove(taskEntity);
        UpdateUI();
    }
    private void Save()
    {
        _EditTaskEntity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        if (oprationType == 0)
        {
            dataManager.InsertTaskEntity(_EditTaskEntity);
            scheduledTask.Add(_EditTaskEntity);
        }
        else
        {
            dataManager.UpdateTaskEntity(_EditTaskEntity);
        }
        this.module.InvokeVoidAsync("CloseDialog", "close");
        Search();

    }
    private string GetIntervalTime(ScheduledTaskEntity taskEntity)
    {
        string time = "";
        if (taskEntity.Year != 0)
        {
            time += taskEntity.Year + " Years,";
        }
        if (taskEntity.Month != 0 || !string.IsNullOrEmpty(time))
        {
            time += taskEntity.Month + " Months,";
        }
        if (taskEntity.Day != 0 || !string.IsNullOrEmpty(time))
        {
            time += taskEntity.Day + " Days,";
        }
        if (taskEntity.Hour != 0 || !string.IsNullOrEmpty(time))
        {
            time += taskEntity.Hour + " Hours,";
        }
        if (taskEntity.Minute != 0 || !string.IsNullOrEmpty(time))
        {
            time += taskEntity.Minute + " Minutes,";
        }
        if (taskEntity.Second != 0 || !string.IsNullOrEmpty(time))
        {
            time += taskEntity.Second + " Seconds";
        }
        return time;
    }

    private string GetTime(ScheduledTaskEntity taskEntity)
    {
        string time = "";
        if (taskEntity.Hour < 10)
        {
            time += "0";
        }
        time += taskEntity.Hour.ToString();
        time += ":";
        if (taskEntity.Minute < 10)
        {
            time += "0";
        }
        time += taskEntity.Minute.ToString();
        time += ":";
        if (taskEntity.Second < 10)
        {
            time += "0";
        }
        time += taskEntity.Second.ToString();
        return time;
    }
    private string GetWeek(int day)
    {
        switch (day)
        {
            case 1:
                return "Monday";
            case 2:
                return "Tuesday";
            case 3:
                return "Wednesday";
            case 4:
                return "Thursday";
            case 5:
                return "Friday";
            case 6:
                return "Saturday";
            case 7:
                return "Sunday";
            default:
                return "";
        }
    }
    private void Search()
    {
        scheduledTask = dataManager.GetScheduledTasks(_Name);
        UpdateUI();
    }
    private void HiddenNav()
    {
        commentTool.ShowNav = false;
    }
    private async void UpdateUI()
    {
        await InvokeAsync(() => StateHasChanged());
    }
}
